package com.globallogic.vacationplanner.web.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

public class HashGenerator {

	private static final Logger LOGGER = Logger.getLogger(HashGenerator.class);

	private HashGenerator() {
	};

	/**
	 * This method encrypting incoming String by the MD5 algorithm
	 * 
	 * @param password
	 * @return String
	 */
	public static String getMD5Hash(String password) {

		LOGGER.info("Encrypting password");
		MessageDigest md5;
		StringBuilder hexString = new StringBuilder();

		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			md5.update(password.getBytes());
			byte messageDigest[] = md5.digest();
			for (int i = 0; i < messageDigest.length; i++) {
				hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
			}
		} catch (NoSuchAlgorithmException e) {
			LOGGER.error("No Such Algorithm Exception", e);
			return e.toString();
		}
		return hexString.toString();
	}

}
